<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
use app\common\library\Letter;
class Special extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['special_addtime'];
	protected $update = [];
	//自动完成相关字段
	protected function setSpecialAddtimeAttr($value,$data){
		if (!empty($data['special_addtime']) || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}
	protected function setSpecialkeywordsAttr($value,$data){
		if(!empty($value)){
			return $value;
		}elseif(empty($value) && !empty($data['special_name']) && !empty($data['auto_keywords'])){
			$get=controller('get');
			$info=$get->keywords($data['special_name'],1);
			if($info['code']=1){
			    return $info['data']['str'];	
			}else{
				return '';
			}
		}else{
			return '';
		}
	}
	protected function setSpecialTagAttr($value,$data){
		if(empty($value) &&  !empty($data['auto_tag'])){
			return zanpian_get_tag($data['special_name'],$data['special_content']);
		}else{
			return $value;
		}
	}
    protected function setSpecialPrtyAttr($value){
        if(!is_array($value)){
            return 0;
        }else{
            $attribute = 0;
            foreach ($value as $key=>$value){
                $attribute += $value;
            }
            return $attribute;
        }
    }	
	protected function setSpecialLetterAttr($value,$data){
		$Letter = new Letter();
		if ($data['special_name']) {
			return $Letter->getletter($data['special_name']);
		}
	}
    protected function setSpecialLettersAttr($value,$data){
	    $Letter = new Letter();
	    if(empty($value)) {
		    return $Letter->getletters(trim($data['special_name']),10);
		}else{
		    return trim($value);
		}
    }
	protected function setSpecialPicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'special');
		return $file['file'];
	}
	protected function setSpecialBigpicAttr($value){
		$img = model('Img');
		$file=$img->down_load(trim($value),'special');
		return $file['file'];
	}	
	protected function setSpecialContentAttr($value){	
	    if(!empty($value)){
	       return zanpian_content_down_images(trim($value),'special');
	    }else{
	       return ''; 	 
	    }
	}
	protected function setSpecialGoldAttr($value){
		if($value > 10){
			$value = 10;
		}	
		return 	$value;
	}	
	//事件注册
    protected static function init(){
		//写入后置事件
		Special::afterWrite(function ($special){
			if(isset($special->special_tag)){
			    model('Tag')->tag_updata($special->special_id,10,$special->special_tag);
			}
			if(!empty($special->special_id)){
			   model('Manage')->DelCache(10,$special->special_id);
			}		
		});			
		//删除后置事件
        Special::afterDelete(function ($special){
			model('Tag')->tag_updata($special->special_id,10);
			zanpian_del_img_file($special->special_content);
			//删除图片和缩略图
            if(!empty($special->special_pic) && file_exists(ROOT_PATH.$special->special_pic)){
                @unlink(ROOT_PATH.$special->special_pic);			
            }
            model('Manage')->DelCache(10,$special->special_id);			
       });	
    }	
	//关联方法
	public function topicvod(){
        return $this->hasMany('Topic','topic_tid')->where(['topic_sid'=>1,'topic_tsid'=>10]);
    }
	public function topicnews(){
        return $this->hasMany('Topic','topic_tid')->where(['topic_sid'=>2,'topic_tsid'=>10]);
    }
	public function topicstar(){
        return $this->hasMany('Topic','topic_tid')->where(['topic_sid'=>3,'topic_tsid'=>10]);
    }	
     //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='special_'.$param['order'].' desc';
		}		
		if(empty(order_check($tag['order']))){
			$tag['order']='special_addtime desc';
		}
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['special_status']=['eq',1];
	    if(!empty($tag['ids'])){
		     $where['special_id'] = array('in',$tag['ids']);
	    }
	    if(!empty($tag['not'])){
		     $where['special_id'] = array('not in',$tag['not']);
	    }		
	    if (!empty($tag['letter'])) {
		    $letter = explode(',',$tag['letter']);
		    if(count($letter)>1) {
			    $where['special_letter'] = array('in',$tag['letter']);
		    }else{
			    $where['special_letter'] = array('eq',$letter[0]);
		    }
	    }		
	    if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['special_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['special_cid'] = getlistsqlin($tag['cid']);
		    }
	    }		
	    if(!empty($tag['stars'])) {
		     $where['special_stars'] = array('in',$tag['stars']);
	    }
	    if (!empty($tag['hits'])) {
		    $hits = explode(',',$tag['hits']);
		    if (count($hits) > 1) {
			    $where['special_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['special_hits'] = array('gt',$hits[0]);
		    }
	    }
	    if(!empty($tag['name'])) {
		    $where['special_name'] = array('like','%'.$tag['name'].'%');	
	    }
	    if(!empty($tag['wd'])) {
		    $where['special_name|special_content'] = array('like','%'.$tag['wd'].'%');	
	    }
        if(!empty($tag['gold'])) {
		    $gold = explode(',',$tag['gold']);
		    if(count($gold) > 1) {
			    $where['special_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['special_gold'] = array('gt',$gold[0]);
		    }
	    }
	    if(!empty($tag['golder'])) {
		    $golder = explode(',',$tag['golder']);
		    if(count($golder) > 1) {
			    $where['special_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['special_golder'] = array('gt',$golder[0]);
		    }
	    }
	    if(!empty($tag['up'])) {
		    $up = explode(',',$tag['up']);
		    if(count($up)>1) {
			    $where['special_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['special_up'] = array('gt',$up[0]);
		    }
	    }
	    if(!empty($tag['down'])) {
		    $down = explode(',',$tag['down']);
		    if(count($down)>1) {
			    $where['special_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['special_down'] = array('gt',$down[0]);
		    }
	    }	
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['special_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['special_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
		if($tag['prty']){
			if(!empty(check_position($tag['prty']))){
			    $where['special_prty'] = check_position($tag['prty']);
			}
		}
        if(!empty($tag['tag'])){
		    $where['tag_name'] = array('eq',$tag['tag']);
		    $where['tag_sid'] = 10;
		    $rs = db('tag')->join('special special','special.special_id =tag_id','RIGHT');	
	    }else{
			$rs=db('special');
		}
        if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = $rs->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = $rs->where($where)->field('special_content',true)->order($tag['order'])->limit($limit_str)->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_special')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 
	
}
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------